package com.it.Utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.it.JDBCUtils.JDBCUtils;
import com.it.Object.Connector;
import com.it.Object.Display;
import com.it.Object.Specification;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ParseDetailedUrl {
    public static int count = 0;
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    private ExecutorService es = Executors.newFixedThreadPool(20);

    public void doUrls() {
        for (int i = 1; i <= 199; i += 2) {
            String url = "https://search.jd.com/Search?keyword=%E6%98%BE%E7%A4%BA%E5%99%A8" +
                    "&wq=%E6%98%BE%E7%A4%BA%E5%99%A8&pvid=dc5545d490f642fcac338560c778422c&s=116&click=0" +
                    "&page=" + i;
            Document doc = null;
            try {
                doc = Jsoup.connect(url)
                        .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36 Edg/92.0.902.84")
                        .get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Elements aTags = doc.select("div.p-img > a");
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    for (Element aTag : aTags) {
                        //详情页url
                        String href = "https:" + aTag.attr("href");
                        //商品图片的url
                        String purl = "https:";// + aTag.select("img").attr("src");
                        try {
                            parseUrl(href, purl);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            es.submit(runnable);
        }
    }

    public void parseUrl(String url, String pUrl) throws Exception {
        Display d = new Display();
        d.setUrl(url);
        d.setpUrl(pUrl);
        Document doc = Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36 Edg/92.0.902.84")
                .get();
        //品牌
        String brand = doc.select("#parameter-brand > li").attr("title");
        d.setBrand(brand);
        //商品价格
        double price = getPrice(url);
        d.setPrice(price);
        //商家描述
        String details = doc.select("div.sku-name").text();
        d.setDetails(details);
        Elements lis = doc.select("div.p-parameter > ul.parameter2.p-parameter-list > li");
        for (Element li : lis) {
            String s = li.text();
            String title = li.attr("title");
            if (s.contains("商品名称")) {
                //商品名称
                String name = title;
                d.setName(name);
            } else if (s.contains("商品编号")) {
                //商品编号
                String productNumber = title;
                d.setProductNumber(productNumber);
            } else if (s.contains("屏幕刷新率")) {
                //刷新率
                if(title.split("Hz")[0].equals("其他")) {
                    d.setFps(0);
                } else {
                    Integer fps = Integer.valueOf(title.split("Hz")[0]);
                    d.setFps(fps);
                }
            } else if (s.contains("屏幕尺寸")) {
                //屏幕尺寸
                Double screenSize = Double.valueOf(title.split("英寸")[0]);
                d.setScreenSize(screenSize);
            } else if (s.contains("分辨率")) {
                //分辨率
                String res = title;
                d.setRes(res);
            } else if (s.contains("面板")) {
                //面板类型
                String panel = title;
                d.setPanel(panel);
            } else if (s.contains("屏幕比例")) {
                //屏幕比例
                String screenRatio = title;
                d.setScreenRatio(screenRatio);
            }
        }
        Elements es = doc.select("div.Ptable-item");
        for (Element e : es) {
            if (e.select("h3").text().equals("规格")) {
                //规格
                Integer specificationId = getSpecificationID(url, e);
                if (specificationId == null) specificationId = 1;
                d.setSpecificationId(specificationId);
            } else if (e.select("h3").text().equals("接口")) {
                //接口
                Integer connectorId = getConnectorID(url, e);
                if (connectorId == null) connectorId = 1;
                d.setConnectorId(connectorId);
            }
        }
        //保存信息
        template.update("insert into display values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                d.getName(), d.getProductNumber(), d.getBrand(), d.getFps(), d.getScreenSize(), d.getRes(),
                d.getPanel(), d.getScreenRatio(), d.getPrice(), d.getDetails(), d.getUrl(), d.getpUrl(),
                d.getSpecificationId(), d.getConnectorId());
        //打印信息
        System.out.println(d.toString());
    }

    /**
     * 京东商品价格保存在json文件中
     *
     * @param url
     * @return
     */
    public Double getPrice(String url) throws Exception {
        //获取商品编号
        String number = url.substring(20).split("\\.")[0];
        //价格页面
        String p = "https://p.3.cn/prices/mgets?skuIds=J_" + number;
        //获取价格页面json
        String json = loadJson(p).substring(1).split("]")[0];
        JSONObject jo = (JSONObject) JSON.parse(json);
        //获取price
        Double price = 0.0;
        price = jo.getDouble("p");
        return price;
    }

    //读取url上的json文件
    public static String loadJson(String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL urlObject = new URL(url);
            URLConnection uc = urlObject.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json.toString();
    }

    /**
     * 获取规定spec的id
     *
     * @param url
     * @param e
     * @return
     * @throws IOException
     */
    public Integer getSpecificationID(String url, Element e) throws IOException {
        Specification sp = new Specification();
        Elements dls = e.select("dl > dl.clearfix");
        for (Element dl : dls) {
            if (dl.select("dt").text().equals("产品尺寸（mm）")) {
                sp.setProductSize(dl.select("dd").text());
            } else if (dl.select("dt").text().equals("电源类型")) {
                sp.setPower_Type(dl.select("dd").text());
            } else if (dl.select("dt").text().equals("底座")) {
                sp.setBase(dl.select("dd").text());
            } else if (dl.select("dt").text().equals("产品净重（kg）")) {
                sp.setWeight(dl.select("dd").text());
            } else if (dl.select("dt").text().equals("壁挂规格")) {
                sp.setWallSpec(dl.select("dd").text());
            } else if (dl.select("dt").text().equals("是否支持壁挂")) {
                sp.setIfHang(dl.select("dd").text());
            }
        }
        List<Specification> list = template.query("select * from Specification;",
                new BeanPropertyRowMapper<Specification>(Specification.class));
        if (!list.contains(sp)) {
            template.update("insert into specification values (null, ?, ?, ?, ?, ?, ?)",
                    sp.getProductSize(), sp.getPower_Type(), sp.getBase(), sp.getWeight(), sp.getWallSpec(), sp.getIfHang());
        }
        List<Map<String, Object>> mapList =
                template.queryForList("select * from specification;");
        Integer id = null;
        for (Map<String, Object> map : mapList) {
            id = (Integer) map.get("id");
        }
        return id;
    }

    /**
     * 获取接口的id
     *
     * @param url
     * @param e
     * @return
     * @throws IOException
     */
    public Integer getConnectorID(String url, Element e) throws IOException {
        Connector con = new Connector();
        Elements dls = e.select("dl > dl.clearfix");
        for (Element dl : dls) {
            if (dl.select("dt").text().equals("音频/耳机输出")) {
                con.setAH(Integer.parseInt(dl.select("dd").text().split("个")[0]));
            } else if (dl.select("dt").text().equals("DP")) {
                con.setDP(Integer.parseInt(dl.select("dd").text().split("个")[0]));
            } else if (dl.select("dt").text().equals("HDMI接口")) {
                con.setHDMI(Integer.parseInt(dl.select("dd").text().split("个")[0]));
            } else if (dl.select("dt").text().equals("VGA")) {
                con.setVGA(Integer.parseInt(dl.select("dd").text().split("个")[0]));
            } else if (dl.select("dt").text().equals("USB扩展/充电")) {
                con.setUSB(Integer.parseInt(dl.select("dd").text().split("个")[0]));
            } else if (dl.select("dt").text().equals("Type-C")) {
                con.setTypeC(Integer.parseInt(dl.select("dd").text().split("个")[0]));
            }
        }
        List<Connector> list = template.query("select * from Connector;",
                new BeanPropertyRowMapper<Connector>(Connector.class));
        if (!list.contains(con)) {
            template.update("insert into connector values (null, ?, ?, ?, ?, ?, ?)"
                    , con.getAH(), con.getDP(), con.getHDMI(), con.getVGA(), con.getUSB(), con.getTypeC());
        }
        List<Map<String, Object>> mapList =
                template.queryForList("select * from connector;");
        Integer id = null;
        for (Map<String, Object> map : mapList) {
            id = (Integer) map.get("id");
        }
        return id;
    }
}
